package com.neteast.androidclient.newscenter.util;


import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;

import com.neteast.androidclient.newscenter.Global;
import com.neteast.androidclient.newscenter.activity.LoginActivity;

public class InfoUtil {
	public static final String APPCODE="10002";
	public static final String KEY="9cf1259a625fd96d";
	
	/**
	 * 设置最新的广播id
	 * @param context
	 * @param infoId
	 */
	public static void setLastBroadCastId(Context context,int userId,long infoId){
		SharedPreferences sp = context.getSharedPreferences(Global.PREFERENCE_CONFIG_FILE, Context.MODE_PRIVATE);
		Editor edit = sp.edit();
		edit.putLong(Global.PREFERENCE_LAST_BROADCAST_ID, infoId);
		edit.commit();
	}
	/**
	 * 得到最新的广播id
	 * @param context
	 * @return
	 */
	public static long getLastBroadCastId(Context context,int userId){
		SharedPreferences sp = context.getSharedPreferences(Global.PREFERENCE_CONFIG_FILE, Context.MODE_PRIVATE);
		return sp.getLong(Global.PREFERENCE_LAST_BROADCAST_ID, 0);
	}
	
	public static void setLastUnicastId(Context context,int userId,long infoId){
		SharedPreferences sp = context.getSharedPreferences(userId+"", Context.MODE_PRIVATE);
		Editor edit = sp.edit();
		edit.putLong(Global.PREFERENCE_LAST_UNICAST_ID, infoId);
		edit.commit();
	}
	/**
	 * 得到最新的广播id
	 * @param context
	 * @return
	 */
	public static long getLastUnicastId(Context context,int userId){
		SharedPreferences sp = context.getSharedPreferences(userId+"", Context.MODE_PRIVATE);
		return sp.getLong(Global.PREFERENCE_LAST_UNICAST_ID, 0);
	}
	/**
	 * 获取用户中心信息
	 * @param context
	 * @return
	 * @throws Exception
	 */
	public static Bundle getUserCenterInfo(Context context) throws Exception{
		String path=LoginActivity.IP+"/Message/userinfo";
		DefaultHttpClient client=new DefaultHttpClient();
		int userId=LoginUtil.getUserId(context);
		String token=LoginUtil.getToken(context);
		HttpPost post=new HttpPost(path);
		ArrayList<NameValuePair> valuePairs=new ArrayList<NameValuePair>();
		
		BasicNameValuePair valuePair=new BasicNameValuePair("appcode",APPCODE);
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("datatype","j");
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("token",token);
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("userid",userId+"");
		valuePairs.add(valuePair);
		
		UrlEncodedFormEntity entity=new UrlEncodedFormEntity(valuePairs, "UTF-8");
		post.setEntity(entity);
		HttpResponse response = client.execute(post);
		HttpEntity httpEntity = response.getEntity();
		if (httpEntity!=null) {
			Bundle userCenterInfo = JsonUtil.parseJson2UserCenterInfo(context, httpEntity.getContent());
			client.getConnectionManager().closeExpiredConnections();
			return userCenterInfo;
		}else {
			throw new RuntimeException("获取用户列表失败");
		}
	}
	/**
	 * 编辑用户中心信息
	 * @param context
	 * @param json
	 * @throws Exception
	 */
	public static void editUserCenterInfo(Context context,JSONObject json) throws Exception{
		String path=LoginActivity.IP+"/Message/useredit";
		DefaultHttpClient client=new DefaultHttpClient();
		int userId=LoginUtil.getUserId(context);
		String token=LoginUtil.getToken(context);
		String verify=MD5Util.phpMD5Encode(userId+""+json.toString()+KEY);
		
		HttpPost post=new HttpPost(path);
		ArrayList<NameValuePair> valuePairs=new ArrayList<NameValuePair>();
		
		BasicNameValuePair valuePair=new BasicNameValuePair("appcode",APPCODE);
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("datatype","j");
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("token",token);
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("userid",userId+"");
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("Json_string",json.toString());
		valuePairs.add(valuePair);
		valuePair=new BasicNameValuePair("verify",verify);
		valuePairs.add(valuePair);
		
		
		UrlEncodedFormEntity entity=new UrlEncodedFormEntity(valuePairs, "UTF-8");
		post.setEntity(entity);
		HttpResponse response = client.execute(post);
		HttpEntity httpEntity = response.getEntity();
		if (httpEntity!=null) {
		}else {
			throw new RuntimeException("获取用户列表失败");
		}
	}
}
